﻿using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using XRM.ActiveDirectoryService;
using XRM.Security.WebUI.Models.Department;
using XRM.Security.WebUI.Models.UserRegister;
using db = XRM.Security.DataAccess.Models;

namespace XRM.Security.WebUI.Models.SystemUser
{
    public class ADUserDialogModel : BaseViewModel, IValidatableObject
    {
        [Display(Name = "Поиск")]
        [MaxLength(50, ErrorMessage = "Максимальная длина поля 50 символов")]
        public string SearchString { get; set; }

        public string SubmitAction { get; set; }
        public string SelectedFullName { get; set; }
        public string SelectedLogin { get; set; }
        
        public List<ADUser> ADUserList
        {
            get; set;
        }

        public void GetDepartmentAll(List<db.Department> listAll)
        {
            DepartmentAllList = new List<DepartmentModel>();

            if (listAll != null && listAll.Any())
            {
                foreach (db.Department item in listAll)
                {
                    DepartmentAllList.Add(new DepartmentModel(item));
                }
            }
            DepartmentAllList.Insert(0, new DepartmentModel() { Id = null, Name = "(Выберите подразделение)" });
        }

        public void GetDepartmentAddressAll(List<db.DepartmentAddress> listAll)
        {
            DepartmentAddressAllList = new List<DepartmentAddressModel>();

            if (listAll != null && listAll.Any())
            {
                foreach (db.DepartmentAddress item in listAll)
                {
                    DepartmentAddressAllList.Add(new DepartmentAddressModel(item));
                }
            }
            DepartmentAddressAllList.Insert(0, new DepartmentAddressModel() { Id = null, Address = "(Выберите адрес)" });
        }

        [Display(Name = "Подразделение")]
        public int? DepartmentId { get; set; }

        [Display(Name = "Адрес")]
        public int? DepartmentAddressId { get; set; }

        public List<DepartmentModel> DepartmentAllList { get; set; }
        public List<DepartmentAddressModel> DepartmentAddressAllList { get; set; }

        public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
        {
            yield break;
        }
    }

    public class ADUserDialogFilterModel : BaseViewModel
    {
        public string UserDialogSubmitAction { get; set; }

        [Display(Name = "Фамилия")]
        [MaxLength(50, ErrorMessage = "Максимальная длина поля 'Фамилия' 50 символов")]
        public string LastNameFilter { get; set; }
        [Display(Name = "Имя")]
        [MaxLength(50, ErrorMessage = "Максимальная длина поля 'Имя' 50 символов")]
        public string FirstNameFilter { get; set; }

        public UserRegisterModel UserRegisterModel
        {
            get; set;
        }
    }
}